package com.pignest.framework.test;

import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import com.pignest.hotel.spring.ContextHolder;

public class SpringTestCase extends BaseTestCase {
	
	private ApplicationContext context = new ClassPathXmlApplicationContext(
			new String[] { "/applicationContext-main.xml" });
	protected SessionFactory sessionFactory;
	protected Session session;

	protected ApplicationContext getContext() {
		return context;
	}
	
	protected void setUp() {
		sessionFactory = (SessionFactory) context.getBean("sessionFactory");
		session = SessionFactoryUtils.getSession(sessionFactory, true);
		session.setFlushMode(FlushMode.AUTO);	//NEVER
		TransactionSynchronizationManager.bindResource(sessionFactory,
				new SessionHolder(session));
		ContextHolder.setApplicationContext(context);
	}

	protected void tearDown() throws Exception {
		TransactionSynchronizationManager.unbindResource(sessionFactory);
		SessionFactoryUtils.releaseSession(session, sessionFactory);
	}
}
